package com.protocols.sctp.m3ua;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.protocols.*;

public class InfoString implements Serializable{
	private OctaveInteger ParameterTag= new OctaveInteger(4,2); // ASP Identifier
	private OctaveInteger ParameterLength= new OctaveInteger(4,2);	// Length=8
	private String InfoString= "";
	private Boolean Visible=false;
	private Boolean Mandatory =true;
	
	public InfoString(Boolean mandatory) {
		Mandatory=mandatory;
	}
	
	public InfoString(String infostring,Boolean mandatory) {		
		InfoString=infostring;
		setLength();
		Mandatory=mandatory;
	}

	
	public byte [] getByteArray(){		
		setLength();
		return  !Visible?new byte[0]:ProtoOperations.concatArrays(ParameterTag.getByteArray(), ParameterLength.getByteArray(), InfoString.getBytes());
		

	}
	

	public int getParameterTag() {
		return getByteArray().length;
	}


	private void setLength() {
		ParameterLength.Value = 4+ InfoString.length();
	}
	
	public int getLength() {
		return Visible?ParameterLength.Value:0;
	}



	public String getInfoString() {
		return InfoString;
	}


	public void setInfoString(String infostring) {
		InfoString = infostring;
		setLength();
		Visible=true;
	}
	public Boolean getVisible() {
		return Visible;
	}

	public void setVisible(Boolean visible) {
		Visible = visible;
	}
	
	
	
	public byte [] decode(byte [] input) throws ProtocolStackException{
		
		if (input.length==0)
			return new byte[0];
		else if (input.length<4 && input.length>0)
			throw new ProtocolStackException("Decode Failure:InfoString"+ProtoOperations.getHexString(input));
		
		
		if (input[0]==0 && input[1]==4)
		{			
			ParameterLength.Value=ProtoOperations.intFromByteArray(input, 2, 2);
			setInfoString(new String( ProtoOperations.extractByteArray(input, 4, ParameterLength.Value-4)));
			return ProtoOperations.extractByteArray(input, ParameterLength.Value, input.length-ParameterLength.Value);
		}
		else if (isMandatory())
			throw new ProtocolStackException("Decode Failure:InfoString"+ProtoOperations.getHexString(input));
		else
			return input;
				
	}

	public Boolean isMandatory() {
		return Mandatory;
	}

	public void setMandatory(Boolean mandatory) {
		Mandatory = mandatory;
	}

	
	
	
}
